import router from '@system.router';
import dataStorage from '@ohos.data.storage'
import ClassApi from '../../api/classApi.js'

export default {
    data: {
        baseImageUrl: '',
        studentId: 0,
        tab: 0,
        fresh: false,
        classes: [],
        classes_default: [],
        All_bool: true,
    },
    onInit() {
        this.baseImageUrl = this.$app.$def.globalData.baseImageUrl;
        ClassApi.getClasses((data) => {
            this.classes = data.rows;
        });
    },
    goBack() {
        router.back();
    },
    changeTabs(index) {
        this.tab = index;
        if (index == 0) {
            ClassApi.getClasses((data) => {
                this.classes = data.rows;
                this.classes_default = data.rows;
                this.All_bool=true;
            });
        } else {
            if (this.studentId == 0) {
                this.getClassesByStudentId(this);
            } else {
                ClassApi.getClassesByStudentId(this.studentId, (data) => {
                    this.classes = data;
                    this.All_bool = false;
                });

            }


        }
    },
    refresh: function (e) {
        let that = this;
        that.fresh = e.refreshing;
        ClassApi.getClasses((data) => {
            this.classes = data.rows;
            that.fresh = false;
        });
    },
    getClassesByStudentId(that) {
        let storage = dataStorage.getStorageSync(this.$app.$def.globalData.path);
        let user = storage.getSync('user', '{“id”:0}');
        ClassApi.getClassesByStudentId(JSON.parse(user).id, (data) => {
            that.classes = data;
            that.All_bool=false;
        });
        //        storage.get({
        //            key: 'user',
        //            success: function (data) {
        //                let studentId = JSON.parse(data).id;
        //                ClassApi.getClassesByStudentId(studentId, (data) => {
        //                    that.classes = data;
        //                });
        //            },
        //            fail: function (data, code) {
        //                console.log('call storage.get fail, code: ' + code + ', data: ' + data);
        //            },
        //            complete: function () {
        //                console.log('call complete');
        //            },
        //        });
    },
    viewDetail(id) {
        router.push({
            uri: 'pages/classDetail/classDetail',
            params: {
                id
            }
        });
    },
    quickSort(arr) {
        if (arr.length <= 1) {
            return arr // 递归的出口
        }

        let flag = arr[0]; // 拿到数组的第一个元素作为flag
        let left = [];
        let right = [];
        let non = [];
        for (let i = 1;i < arr.length; i++) {
            if (parseInt(arr[i].studentNum) < parseInt(flag.studentNum)) {
                left.push(arr[i])
            } else if (parseInt(arr[i].studentNum) > parseInt(flag.studentNum)) {
                right.push(arr[i])
            } else {
                non.push(arr[i])
            }
        }
        return ((this.quickSort(right).concat(flag)).concat(this.quickSort(left))).concat(non) // 递归
    },
    SortByNum() {
        this.classes = this.quickSort(this.classes);
    },
    changeFruit(e) {
        if (e.newValue == 'byNum') {
            this.SortByNum();
        } else {
            this.classes = this.classes_default;
        }
    }
}
